//
//  ProductManagerVC.m
//  LazyCat
//
//  Created by linhui on 15/12/24.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "ProductManagerVC.h"
#import "TabObjectProduct.h"
#import "ClassifyManageViewController.h"
#import "ProductAllEditVC.h"
#import "SearchViewController.h"
#import "AddProductViewController.h"
@interface ProductManagerVC ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,UIScrollViewDelegate>
{
    UIPageViewController *_pageViewCtrl;
    NSMutableArray *_dataArray;
    NSInteger _currentPage;
    UIView *_bgView;
    UIView *_lineView;
}
@property (nonatomic,strong)UIView *bottomViewTwoBtn;
@property (nonatomic,strong)UIView *bottomViewEdit;

@end

@implementation ProductManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    [AppData share].productManagerVC = self;
    [self createUI];
    [self createBottomView];
    [self configPageViewController];
    
}
- (void)createBottomView
{
    /*
     bottomViewOne
     
     */
    self.bottomViewTwoBtn = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - 50, ScreenWidth, 50)];
    self.bottomViewTwoBtn.backgroundColor = [UIColor whiteColor];
    self.bottomViewTwoBtn.hidden = NO;
    [self.view addSubview:self.bottomViewTwoBtn];
    
    // bottomBtn
    NSArray *btnArr = @[@"添加商品",@"批量管理"];
    CGFloat height = 40;
    CGFloat space = 10;
    CGFloat width = (ScreenWidth - (space * 3)) / 2.0;
    for (int i = 0; i < btnArr.count; i++) {
        UIButton *bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        bottomBtn.frame = CGRectMake(space + i * (width + space), 5, width, height);
        [bottomBtn setTitle:btnArr[i] forState:UIControlStateNormal];
        [bottomBtn setTitleColor:i == 0 ? [UIColor whiteColor] : MainColor forState:UIControlStateNormal];
        [bottomBtn setBackgroundColor:i == 0 ? MainColor : [UIColor whiteColor]];
        bottomBtn.titleLabel.font = FontSet(16);
        bottomBtn.layer.cornerRadius = 5;
        bottomBtn.layer.masksToBounds = YES;
        bottomBtn.layer.borderWidth = 1;
        bottomBtn.layer.borderColor = MainColor.CGColor;
        [bottomBtn addTarget:self action:@selector(bottomBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        bottomBtn.tag = 7 + i;
        [self.bottomViewTwoBtn addSubview:bottomBtn];
    }
}

- (void)createUI{
    self.view.backgroundColor = BackColor;
    if ([self.isHotVC isEqualToString:@"1"])
    {
       [self initNav:@"热门商品" doBack:YES];
    }else
    {
        [self initNav:@"商品管理" doBack:YES];
    }
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    lineView.alpha = 0.2;
    [self.navBar addSubview:lineView];
    
    //搜索按钮
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(ScreenWidth - 52, 30, 42, 30);
    [searchBtn addTarget:self action:@selector(goSearchViewController) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setImage:[UIImage imageNamed:@"searchLog"] forState:UIControlStateNormal];
    [searchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [searchBtn setImageEdgeInsets:UIEdgeInsetsMake(4, 10, 4, 10)];
    [self.navBar addSubview:searchBtn];
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 40)];
    _bgView.backgroundColor = SetColor(248, 248, 248, 1);
    [self.view addSubview:_bgView];
    _bgView.alpha = 1;
    UIView *buttomLine = [[UIView alloc]initWithFrame:CGRectMake(0, 104, ScreenWidth, 1)];
    buttomLine.backgroundColor = BackColor;
    buttomLine.alpha = 0.2;
    [self.view addSubview:buttomLine];
    _dataArray = [[NSMutableArray alloc]init];
    NSArray *array;
    
    array = @[@"在售商品",@"已下架商品"];
    for(int i=0;i<array.count;i++) {
        TabObjectProduct *tab = [[TabObjectProduct alloc]init];
        tab.title = array[i];
        tab.subViewController = [[SubProductManagerViewController alloc]init];
        tab.subViewController.currentPage = i;
        [_dataArray addObject:tab];
        tab.subViewController.isHotVC = _isHotVC;
        __weak typeof(self)wSelf = self;
        [tab.subViewController setMyBlock:^(UIViewController *detailVC) {
            [wSelf.navigationController pushViewController:detailVC animated:YES];
        }];
    }
    _currentPage = 0;
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = MainColor;
    [_bgView addSubview:_lineView];
    for(int i=0;i<_dataArray.count;i++){
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*(ScreenWidth/_dataArray.count), 0, ScreenWidth/_dataArray.count, 40);
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:SetColor(51, 51, 51, 1) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:MainColor forState:UIControlStateSelected];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn addTarget:self action:@selector(changeTab:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + i;
        [_bgView addSubview:btn];
        if(i == _currentPage){
            btn.selected = YES;
            _lineView.frame = CGRectMake(i*(ScreenWidth/2)+40, 38, ScreenWidth/2-80, 2);
        }
    }
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self   action:@selector(hideKeyB)];
    [self.navBar addGestureRecognizer:tap];
}
- (void)hideKeyB {
    [self.view endEditing:YES];
}

- (void)configPageViewController{
    _pageViewCtrl = [[UIPageViewController alloc]initWithTransitionStyle:(UIPageViewControllerTransitionStyleScroll) navigationOrientation:(UIPageViewControllerNavigationOrientationHorizontal) options:nil];
    SubProductManagerViewController * sub = [_dataArray[_currentPage] subViewController];
    
    [_pageViewCtrl setViewControllers:@[sub] direction:(UIPageViewControllerNavigationDirectionForward) animated:YES completion:^(BOOL finished) {
        
    }];
    _pageViewCtrl.view.frame = CGRectMake(0, 40+65, ScreenWidth, ScreenHeight-65-40 - 50);
    [self.view addSubview:_pageViewCtrl.view];
    _pageViewCtrl.dataSource = self;
    _pageViewCtrl.delegate = self;
    //获取显示所有view的滚动视图
    UIScrollView *sv = _pageViewCtrl.view.subviews[0];
    sv.delegate = self;
    [_pageViewCtrl.view.subviews[0] setDelegate:self];
}
//单击按钮时切换视图
- (void)changeTab:(UIButton *)sender {
    NSInteger index = sender.tag-100;
    SubProductManagerViewController *sub = [_dataArray[index] subViewController];
    __weak typeof(_bgView)wBgView = _bgView;
    __weak typeof(_lineView)wLineView = _lineView;
    //显示翻页后要显示sub，翻页方向根据单击的按钮决定
    [_pageViewCtrl setViewControllers:@[sub] direction:_currentPage>index animated:YES completion:^(BOOL finished) {
        _currentPage = index;
        for(UIView *view in wBgView.subviews){
            if([view isKindOfClass:[UIButton class]]){
                UIButton *tb = (UIButton *)view;
                if(tb.tag == sender.tag){
                    tb.selected=YES;
                }else {
                    tb.selected = NO;
                }}}
        wLineView.frame = CGRectMake(sender.frame.origin.x+40, 38, ScreenWidth/2-80, 2);
    }];
}
#pragma mark --
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    SubProductManagerViewController *sub = (SubProductManagerViewController *)viewController;
    NSInteger index = sub.currentPage;
    index++;
    if(index>=_dataArray.count){
        index = 0;
        return nil;
    }
    SubProductManagerViewController *rsvc = [_dataArray[index]subViewController];
    return rsvc;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    SubProductManagerViewController *sub = (SubProductManagerViewController *)viewController;
    NSInteger index = sub.currentPage;
    index--;
    if(index<0){
        index = _dataArray.count-1;
        return nil;
    }
    SubProductManagerViewController *rsvc = [_dataArray[index]subViewController];
    return rsvc;
    
}
//翻页成功后执行的方法
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
    SubProductManagerViewController *sub = (SubProductManagerViewController *)pageViewController.viewControllers[0];
    _currentPage = sub.currentPage;
    for(UIView *view in _bgView.subviews){
        if([view isKindOfClass:[UIButton class]]){
            UIButton *btn = (UIButton *)view;
            if(btn.tag-100==_currentPage){
                btn.selected =YES;
            }else {
                btn.selected = NO;
            }
        }
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offSetX = scrollView.contentOffset.x;
    CGFloat ratio = (ScreenWidth - 80)/2/ScreenWidth;
    CGRect frame = _lineView.frame;
    frame.origin.x = (offSetX-ScreenWidth)*ratio+_currentPage*ScreenWidth/2+40;
    _lineView.frame = frame;
}


#pragma mark -- Clicked..
//分类按钮点击
- (void)categoryBtnClick {
    ClassifyManageViewController *classman = [[ClassifyManageViewController alloc] init];
    [self.navigationController pushViewController:classman animated:YES];
}

- (void)goSearchViewController
{
    SearchViewController *searchVC = [[SearchViewController alloc]init];
    searchVC.searchType = TypeProductsManager;
    searchVC.isHotVC = self.isHotVC;
    [self.navigationController pushViewController:searchVC animated:YES];
}

//bottomView两个按钮点击事件 (tag == 7,8)
- (void)bottomBtnClicked:(UIButton *)sender
{
    if (sender.tag == 7) {
//        NSLog(@"添加商品");
//        if (self.editClickBlock) {
//            self.editClickBlock();
//        }
        AddProductViewController *addProcuctVC = [[AddProductViewController alloc]init];
        [self.navigationController pushViewController:addProcuctVC animated:YES];
    }else if (sender.tag == 8){
//        self.bottomViewTwoBtn.hidden = YES;
//        self.bottomViewEdit.hidden = NO;
        ProductAllEditVC *allEditVC = [[ProductAllEditVC alloc]init];
        allEditVC.currentPage = _currentPage;
        allEditVC.isHotVC = self.isHotVC;
        [self.navigationController pushViewController:allEditVC animated:YES];
        
    }
}
@end
